CORELIB = $(patsubst %.c, %.o, $(shell find ./src -type f -name "*.c"))
MODULES = $(patsubst %.c, %.o, $(shell find ./modules -type f -name "*.c"))

LIBS = -lm -ldl -lpthread
JNIDIR = -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux
INCLUDES = -I. -Iinclude/compiler -Iinclude/engine -Iinclude/objects -Iinclude/utils -Iinclude/rssb $(JNIDIR)

CC = gcc
CPP = g++
CFLAGS = -Wall -Wno-address -Wno-strict-aliasing -Wno-unused-but-set-variable -O3 -std=gnu11 -fPIC -fmessage-length=0
CPPFLAGS = -Wall -Wno-address -Wno-strict-aliasing -Wno-unused-but-set-variable -O3 -std=gnu++11 -fPIC -fmessage-length=0

.PHONY: all clean core main modules rssb

all: core main modules

clean:
	rm -f ./main.o $(CORELIB) $(MODULES)
	rm -f *.so *.dll *.dylib *.redmod RedScript

core: $(CORELIB)
	$(CC) $(CORELIB) -o libRedScript.so -shared $(LIBS)

main: core main.o
	$(CC) main.o -o RedScript -lRedScript -lreadline -L.

modules: $(MODULES)
	$(CC) modules/sys.o       -o sys.so       -shared -lRedScript -L.
	$(CC) modules/ffi.o       -o ffi.so       -shared -lRedScript -L. -ldl -lffi
	$(CC) modules/signal.o    -o signal.so    -shared -lRedScript -L.
	$(CC) modules/threading.o -o threading.so -shared -lRedScript -L. -lpthread

rssb: rssb/rssb.o rssb/rssb_types.o
	$(CPP) rssb/rssb.o rssb/rssb_types.o -o librssb.so -shared -lRedScript -L.

%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

%.o: %.cpp
	$(CPP) $(CPPFLAGS) $(INCLUDES) -c $< -o $@
